package com.xxx.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxx.common.Result;
import com.xxx.entity.MPage;
import com.xxx.entity.Order;
import com.xxx.entity.query.OrderQuery;
import com.xxx.entity.save.OrderSave;
import com.xxx.service.IOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xxx
 * @since 2024-11-24
 */
@RestController
@RequestMapping("/order")
@Tag(name = "订单管理")
public class OrderController {

    @Autowired
    private IOrderService orderService;

    @PostMapping("/add")
    @Operation(summary = "添加订单")
    public Result add(@RequestBody Order order){
        Boolean flag = orderService.save(order);
        return flag ? Result.success("添加订单成功") : Result.error("添加订单失败");
    }

    @GetMapping("/delete/{orderId}")
    @Operation(summary = "删除订单")
    public Result delete(@PathVariable Integer orderId){
        Boolean flag = orderService.removeById(orderId);
        return flag ? Result.success("删除订单成功") : Result.error("不存在该订单Id");
    }

    @PutMapping("/update")
    @Operation(summary = "修改订单")
    public Result update(@RequestBody Order order){
        Boolean flag = orderService.updateById(order);
        return flag ? Result.success("修改订单成功") : Result.error("修改订单失败");
    }

    @GetMapping("/selectByPage/{current}/{size}")
    @Operation(summary = "分页查询订单")
    public Result selectByPage(@PathVariable Integer current, @PathVariable Integer size){
        MPage page = orderService.selectByPage(current,size);
        return Result.success(page);
    }

    @PostMapping("/selectByCondition")
    @Operation(summary = "根据状态、用户Id、订单Id条件查询订单")
    public Result selectByCondition(@RequestBody OrderQuery orderQuery){
//        LambdaQueryWrapper<Order> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(orderQuery.getOrderStatus()!=null,
//                        Order::getOrderStatus,orderQuery.getOrderStatus())
//                .eq(orderQuery.getUserId()!=null,
//                        Order::getUserId,orderQuery.getUserId())
//                .eq(orderQuery.getOrderId()!=null,Order::getOrderId,orderQuery.getOrderId());
//        IPage<Order> page = orderService.page(new Page<>(orderQuery.getCurrent(), orderQuery.getSize()),
//                lqw);
        MPage page = orderService.selectByCondition(orderQuery);
        return Result.success(page);
    }


    @PostMapping("/addProduces")
    @Operation(summary = "添加订单并批量添加农产品")
    public Result add(@RequestBody OrderSave orderSave){
        Boolean flag = orderService.addProduces(orderSave);
        return flag ? Result.success("添加订单成功") : Result.error("添加订单失败");
    }
}

